package com.xuxin.baby.model;

import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.bean.UpdateOrderBean;
import com.xuxin.baby.contract.OrderCommitContract;
import com.xuxin.baby.network.Retrofit;
import com.xuxin.baby.network.RetrofitApi;
import com.xuxin.baby.network.myObserver;
import com.xuxin.baby.presenter.OrderCommitPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by xu_xin
 * Created on 2020/10/23 0023
 * 提交订单
 */
public class OrderCommitModel implements OrderCommitContract.Model {

    @Override
    public void updateOrder(OrderCommitPresenter presenter, String tenantId, String token, String memberId, RequestBody requestBody) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .updateOrder(tenantId, token, "1", memberId, requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<UpdateOrderBean>() {
                    @Override
                    public void onNextX(@NonNull UpdateOrderBean updateOrderBean) {
                        if (updateOrderBean.isSuccess()) {
                            presenter.onSuccess(updateOrderBean);
                        } else {
                            presenter.onError(updateOrderBean);
                        }
                    }

                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }

    @Override
    public void updateOrderStatus(OrderCommitPresenter presenter, String tenantId, String token, String orderId, String status,String paymentMtd,String tradeType) {
        try {
            Retrofit.getInstance().createReq(RetrofitApi.class)
                    .updateOrderStatus(tenantId, token, "1", orderId, status,paymentMtd,tradeType)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new myObserver<BaseBean>() {
                        @Override
                        public void onNextX(@NonNull BaseBean baseBean) {
                            if (baseBean.isSuccess()) {
                                presenter.onSuccess(baseBean);
                            } else {
                                presenter.onError(baseBean);
                            }
                        }

                        @Override
                        public void onError(BaseBean baseBean) {
                            presenter.onError(baseBean);
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            presenter.onError(null);
                        }

                        @Override
                        public void onComplete() {
                            presenter.onComplete();
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}